package com.haochang.chunkvod.controller.activity.order;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentCode;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.CollectionUtils;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.order.WeChatInfo;
import com.haochang.chunkvod.model.order.WeChatOrderData;
import com.haochang.chunkvod.tools.vod.log.Logger;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

/**
 * @author qintao
 *         created at 2016/9/9 11:28
 *         微信点餐信息列表
 */

public class OrderWeChatListActivity extends BaseActivity
{
	private TitleView titleView;
	private BaseListView listView;
	private QuickAdapter dataAdapter;
	private List<WeChatInfo> weChatInfoList = new ArrayList<WeChatInfo>();
	private WeChatOrderData weChatOrderData;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}

	@Override
	public void receiveParam()
	{

	}

	@Override
	public void initData()
	{
		weChatOrderData = new WeChatOrderData(OrderWeChatListActivity.this);
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_order_we_chat_list);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(R.string.order_weChat).setRightIconVISIBLE(false).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
			}

			@Override
			public void onDoneClick()
			{

			}
		});
		listView = (BaseListView) findViewById(R.id.listView);
		dataAdapter = new QuickAdapter<WeChatInfo>(OrderWeChatListActivity.this, R.layout.item_we_chat_order_list_layout, weChatInfoList)
		{

			@Override
			protected void convert(BaseAdapterHelper helper, WeChatInfo item)
			{
				helper.setText(R.id.tv_roomNum, item.getRoomNum());
				if (item.getOrderNum() > 1)
				{
					helper.setText(R.id.tv_order_num, item.getOrderNum() + "");
					helper.getView(R.id.tv_order_num).setVisibility(View.VISIBLE);
				} else
					helper.getView(R.id.tv_order_num).setVisibility(View.GONE);
				helper.setText(R.id.tv_payTime, TimeUtils.getMinutesString(item.getPayTime()));
				helper.getView().setTag(R.id.order_weiXin_list, item);
				helper.getView().setOnClickListener(onBaseClickListener);
			}
		};
		listView.setVisibility(View.VISIBLE);
		listView.setAdapter(dataAdapter);
		weChatOrderData.requestWeChatOrderList(new WeChatOrderData.IWeChatOrderListListener()
		{
			@Override
			public void onRequestSuccess(List<WeChatInfo> list)
			{
				if (!CollectionUtils.isEmpty(list))
				{
					weChatInfoList.clear();
					weChatInfoList.addAll(list);
					dataAdapter.replaceAll(weChatInfoList);
				}
			}
		});
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			WeChatInfo weChatInfo = (WeChatInfo) v.getTag(R.id.order_weiXin_list);
			onOrderWeChatClick(weChatInfo.getRoomNum(), weChatInfo.getBillId());
		}
	};

	/**
	 * 微信点餐确认详情
	 */
	private void onOrderWeChatClick(String roomNum, String billId)
	{
		Intent intent = new Intent(OrderWeChatListActivity.this, OrderWeChatDetailActivity.class);
		intent.putExtra(IntentKey.ROOM_NUM, roomNum);
		intent.putExtra(IntentKey.BILL_ID, billId);
		startActivityForResult(intent, IntentCode.WE_CHAT_ORDER_REQUEST);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == IntentCode.WE_CHAT_ORDER_REQUEST && resultCode == RESULT_OK && data != null)
		{
			String billId = data.getStringExtra(IntentKey.BILL_ID);
			if (TextUtils.isEmpty(billId))
				return;
			for (int i = 0; i < weChatInfoList.size(); i++)
			{
				WeChatInfo weChatInfo = weChatInfoList.get(i);
				if (billId.equals(weChatInfo.getBillId()))
				{
					dataAdapter.remove(weChatInfo);
					break;
				}
			}
		}
	}
}
